package P150;

import cn.hutool.json.JSONUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LiangMu
 * @version \$Id: P29.java, v 0.1, 2024年07月05日 17:05 LiangMu Exp $$
 */
public class P27 {

    public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        Set<String> set = new HashSet<>();
        for (int i = 0; i < nums.length - 2; i++) {
            int target = nums[i];
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                int dc = nums[left] + nums[right] + target;
                if (dc == 0) {
                    set.add(target + "|" + nums[left] + "|" + nums[right]);
                    left++;
                    right--;
                    continue;
                }
                if (dc > 0) {
                    --right;
                } else {
                    ++left;
                }
            }
        }
        return set.stream().map(it -> {
            String[] split = it.split("\\|");
            List<Integer> list = new ArrayList<>();
            list.add(Integer.parseInt(split[0]));
            list.add(Integer.parseInt(split[1]));
            list.add(Integer.parseInt(split[2]));
            return list;
        }).collect(Collectors.toList());
    }


    public static void main(String[] args) {
        int[] ints = {-1,0,1,2,-1,-4};
        System.out.println(JSONUtil.toJsonStr(threeSum(ints)));
    }
}
